package com.pz.cloud.framework.society.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pz.cloud.framework.business.society.entity.OrgSociety;
import com.pz.cloud.framework.business.society.entity.OrgSocietyUser;
import com.pz.cloud.framework.business.society.service.IOrgSocietyService;
import com.pz.cloud.framework.business.society.service.IOrgSocietyUserService;
import com.pz.cloud.framework.business.stdGroup.entity.OrgStdGroupUser;
import com.pz.cloud.framework.business.workflow.annotation.AssembleDealFlag;
import com.pz.cloud.framework.util.WorkflowUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;


@RequestMapping("org/societyUser")
@RestController
@Api(tags = "社团用户表")
@Slf4j
public class OrgSocietyUserController extends BaseController<OrgSocietyUser, IOrgSocietyUserService> {
    @Autowired
    private IOrgSocietyService orgSocietyService;
    /**
     * 分页列表查询
     *
     * @param entity
     * @param req
     * @return
     */
    @AutoLog(value = "社团人员表-分页列表查询")
    @ApiOperation(value = "社团人员表-分页列表查询", notes = "社团人员表-分页列表查询")
    @GetMapping(value = "/list")
    @AssembleDealFlag
    @PermissionData(pageComponent = "society/SocietyMember")
    public Result<IPage<OrgSocietyUser>> list(OrgSocietyUser entity,
                                                       HttpServletRequest req) {
        Result<IPage<OrgSocietyUser>> result = new Result<>();
        IPage<OrgSocietyUser> page = service.findPage(entity);
        result.setSuccess(true);
        result.setResult(page);
        return result;
    }

    /**
     * 分页列表查询
     *
     * @param entity
     * @param req
     * @return
     */
    @AutoLog(value = "社团人员申请-分页列表查询")
    @ApiOperation(value = "社团人员申请-分页列表查询", notes = "社团人员申请-分页列表查询")
    @GetMapping(value = "/applyList")
    @AssembleDealFlag
    @PermissionData(pageComponent = "society/NewMember")
    public Result<IPage<OrgSocietyUser>> applyList(OrgSocietyUser entity,
                                              HttpServletRequest req) {
        Result<IPage<OrgSocietyUser>> result = new Result<>();
        IPage<OrgSocietyUser> page = service.findApplyPage(entity);
        result.setSuccess(true);
        result.setResult(page);
        return result;
    }

    /**
     * 添加
     * @return
     */
    @AutoLog(value = "社团人员表-添加|编辑")
    @ApiOperation(value = "社团人员表-添加|编辑", notes = "社团人员表-添加|编辑")
    @PostMapping(value = "/add")
    public Result add(@RequestBody OrgSocietyUser entity) {
        Result<OrgSocietyUser> result = new Result<>();
        service.save(entity);
        result.success("保存成功！");
        return result;
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "社团人员表-通过id删除")
    @ApiOperation(value = "社团人员表-通过id删除", notes = "社团人员表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam(name = "id", required = true) String id) {
        try {
            service.removeById(id);
            WorkflowUtil.deleteProcess(id);//删除流程数据
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "社团人员表-批量删除")
    @ApiOperation(value = "社团人员表-批量删除", notes = "社团人员表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result deleteBatch(@RequestParam(name = "ids") String ids) {
        Result<OrgSocietyUser> result = new Result<OrgSocietyUser>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            service.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "社团人员表-通过id查询")
    @ApiOperation(value = "社团人员表-通过id查询", notes = "职务表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OrgSocietyUser> queryById(@RequestParam(name = "id") String id) {
        Result<OrgSocietyUser> result = new Result<OrgSocietyUser>();
        OrgSocietyUser orgSocietyUser = service.getById(id);
        if (orgSocietyUser == null) {
            result.error500("未找到对应实体");
        } else {
            OrgSociety orgSociety = orgSocietyService.getById(orgSocietyUser.getSocietyId());
            if(orgSociety!=null){
                orgSocietyUser.setSocietyName(orgSociety.getName());
            }
            result.setResult(orgSocietyUser);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 导出excel
     *
     * @param request
     */
    @RequestMapping(value = "/exportXls")
    @PermissionData(pageComponent = "society/SocietyMember")
    public ModelAndView exportXls(HttpServletRequest request, OrgSocietyUser bean) {
        bean.setPaging(false);
        IPage<OrgSocietyUser> pageList = service.findPage(bean);
        return super.exportXls(request, pageList.getRecords(), OrgSocietyUser.class, "校级组织成员");

    }
}
